/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.autoCar.user;

import br.com.autoCar.acessorio.Acessorio;
import br.com.autoCar.banco.ConnectionFactory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Administrator
 */
public class UsuarioDAO {
    Connection con = null;
        public UsuarioDAO() {
        try {
            con = new ConnectionFactory().getConnection();
        } catch (SQLException ex) {
            throw new RuntimeException(ex);
        }
        }
    
    public void inserirUsuario(Usuario usuario) {
        if (!existeUsuario()){
        StringBuilder sql = new StringBuilder();
        sql.append("insert into Usuario (nome,email,senha,endereco,complemento,bairro,cidade")
                .append(",estado,dataNascimento)")
                .append("values (?,?,md5(?),?,?,?,?,?,?)");
        try{
            PreparedStatement stmt = con.prepareStatement(sql.toString());
            
 
            stmt.setString(1, usuario.getNome());
            stmt.setString(2, usuario.getEmail());
            stmt.setString(3, usuario.getSenha());
            stmt.setString(4, usuario.getEndereco());
            stmt.setString(5, usuario.getComplemento());
            stmt.setString(6, usuario.getBairro());
            stmt.setString(7, usuario.getCidade());
            stmt.setString(8, usuario.getEstado());
            
            java.sql.Date date = new java.sql.Date(usuario.getDataNascimento().getTimeInMillis());
            
            stmt.setDate(9, date);
            
            stmt.execute();
            stmt.close();
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }} 
    }
    public void deletarUsuario(Usuario usuario) {
       StringBuilder sql = new StringBuilder();
        sql.append("delete from Usuario ")
                .append(" where idUsuario")
                .append(" = ?");
        try{
            PreparedStatement stmt = con.prepareStatement(sql.toString());
            
 
            stmt.setInt(1, usuario.getId());
            
            stmt.execute();
            stmt.close();
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void updateUsuario(Usuario usuario){
       StringBuilder sql = new StringBuilder();
       sql.append("update Usuario set nome = ?")
               .append("email = ?")
               .append("senha = md5(?)")
               .append("endereco = ?")
               .append("complemento = ?")
               .append("bairro = ?")
               .append("cidade = ?")
               .append("estado = ?")
               .append("dataNascimento = ?");
       
       PreparedStatement stmt = null;
        try {
            stmt.setString(1, usuario.getNome());
            stmt.setString(2, usuario.getEmail());
            stmt.setString(3, usuario.getSenha());
            stmt.setString(4, usuario.getEndereco());
            stmt.setString(5, usuario.getComplemento());
            stmt.setString(6, usuario.getBairro());
            stmt.setString(7, usuario.getCidade());
            stmt.setString(8, usuario.getEstado());
            
            java.sql.Date date = new java.sql.Date(usuario.getDataNascimento().getTimeInMillis());
            
            stmt.setDate(9, date);
            
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
       
       
    }
    public List<Acessorio> buscarAcessorioPorNome(Acessorio acessorio){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from Acessorio")
                .append(" where nomeAcessorio")
                .append(" = ?");
        List<Acessorio> lista = new ArrayList();
        try {
            PreparedStatement stmt = con.prepareStatement(sql.toString());
            stmt.setString(1, acessorio.getNomeAcessorio());
            
            ResultSet rs = stmt.executeQuery();
            
            while(rs.next()) {
                acessorio = new Acessorio();
                acessorio.setIdAcessorio(rs.getInt("idAcessorio"));
                acessorio.setNomeAcessorio(rs.getString("nomeAcessorio"));
                acessorio.setQuantidadeEstoque(rs.getInt("quantidadeEstoque"));
                acessorio.setTamanho(rs.getInt("tamanho"));
                acessorio.setValorAcessorio(rs.getDouble("valorAcessorio"));
                
                lista.add(acessorio);
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return lista;
    }
    public Acessorio buscarAcessorioPorId(int id){
        StringBuilder sql = new StringBuilder();
        sql.append("select * from Acessorio")
                .append(" where idAcessorio")
                .append(" = ?");
         Acessorio acessorio = new Acessorio();
        try {
            PreparedStatement stmt = con.prepareStatement(sql.toString());
            stmt.setInt(1, id);
            
            ResultSet rs = stmt.executeQuery();
            
            if(rs.next()) {
              
                acessorio.setIdAcessorio(rs.getInt("idAcessorio"));
                acessorio.setNomeAcessorio(rs.getString("nomeAcessorio"));
                acessorio.setQuantidadeEstoque(rs.getInt("quantidadeEstoque"));
                acessorio.setTamanho(rs.getInt("tamanho"));
                acessorio.setValorAcessorio(rs.getDouble("valorAcessorio"));
                
                
            }
            
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return acessorio;
    }
}
